package jeesample.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.SQLException;

public final class ExceptionUtils {
	public static String toString(SQLException se) {
		int count = 1;
		java.io.StringWriter sw = new StringWriter();
		java.io.PrintWriter pw = new PrintWriter(sw);
		while (se != null) {
			pw.println("SQLException #" + count);
			pw.println("Code: " + se.getErrorCode());
			pw.println("SqlState: " + se.getSQLState());
			pw.println("Error Message: " + se.getMessage());
			pw.flush();
			se = se.getNextException(); // <<< Can be nested.
			count++;
		}
		return sw.toString();
	}
	
	public static String toString(Throwable th) {
		java.io.StringWriter sw = new StringWriter();
		java.io.PrintWriter pw = new PrintWriter(sw);
		th.printStackTrace(pw);
		return sw.toString();
	}
	
}
